package com.ruoyi.report_wjjhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_wjjhl.domain.bo.SnhshjlBaDetailBo;
import com.ruoyi.report_wjjhl.domain.vo.SnhshjlBaDetailVo;
import com.ruoyi.report_wjjhl.domain.SnhshjlBaDetail;
import com.ruoyi.report_wjjhl.mapper.SnhshjlBaDetailMapper;
import com.ruoyi.report_wjjhl.service.ISnhshjlBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 土报告详细Service业务层处理
 *
 * @author zyb
 * @date 2024-08-02
 */
@RequiredArgsConstructor
@Service
public class SnhshjlBaDetailServiceImpl implements ISnhshjlBaDetailService {

    private final SnhshjlBaDetailMapper baseMapper;

    /**
     * 查询土报告详细
     */
    @Override
    public SnhshjlBaDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询土报告详细列表
     */
    @Override
    public TableDataInfo<SnhshjlBaDetailVo> queryPageList(SnhshjlBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SnhshjlBaDetail> lqw = buildQueryWrapper(bo);
        Page<SnhshjlBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询土报告详细列表
     */
    @Override
    public List<SnhshjlBaDetailVo> queryList(SnhshjlBaDetailBo bo) {
        LambdaQueryWrapper<SnhshjlBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SnhshjlBaDetail> buildQueryWrapper(SnhshjlBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SnhshjlBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, SnhshjlBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(bo.getQualId() != null, SnhshjlBaDetail::getQualId, bo.getQualId());
        lqw.eq(StringUtils.isNotBlank(bo.getSymbolValue()), SnhshjlBaDetail::getSymbolValue, bo.getSymbolValue());
        lqw.eq(bo.getNum1() != null, SnhshjlBaDetail::getNum1, bo.getNum1());
        lqw.eq(bo.getNum2() != null, SnhshjlBaDetail::getNum2, bo.getNum2());
        lqw.eq(StringUtils.isNotBlank(bo.getJiszb()), SnhshjlBaDetail::getJiszb, bo.getJiszb());
        lqw.eq(bo.getJiancjg() != null, SnhshjlBaDetail::getJiancjg, bo.getJiancjg());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegpd()), SnhshjlBaDetail::getJiegpd, bo.getJiegpd());
        lqw.eq(StringUtils.isNotBlank(bo.getQuywz()), SnhshjlBaDetail::getQuywz, bo.getQuywz());
        lqw.eq(bo.getShiyzl1() != null, SnhshjlBaDetail::getShiyzl1, bo.getShiyzl1());
        lqw.eq(bo.getShiyzl2() != null, SnhshjlBaDetail::getShiyzl2, bo.getShiyzl2());
        lqw.eq(bo.getChuds1() != null, SnhshjlBaDetail::getChuds1, bo.getChuds1());
        lqw.eq(bo.getChuds2() != null, SnhshjlBaDetail::getChuds2, bo.getChuds2());
        lqw.eq(bo.getMods1() != null, SnhshjlBaDetail::getMods1, bo.getMods1());
        lqw.eq(bo.getMods2() != null, SnhshjlBaDetail::getMods2, bo.getMods2());
        lqw.eq(bo.getDidl1() != null, SnhshjlBaDetail::getDidl1, bo.getDidl1());
        lqw.eq(bo.getDidl2() != null, SnhshjlBaDetail::getDidl2, bo.getDidl2());
        lqw.eq(bo.getPingjz() != null, SnhshjlBaDetail::getPingjz, bo.getPingjz());
        lqw.eq(bo.getJiehljl() != null, SnhshjlBaDetail::getJiehljl, bo.getJiehljl());
        return lqw;
    }

    /**
     * 新增土报告详细
     */
    @Override
    public Boolean insertByBo(SnhshjlBaDetailBo bo) {
        SnhshjlBaDetail add = BeanUtil.toBean(bo, SnhshjlBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改土报告详细
     */
    @Override
    public Boolean updateByBo(SnhshjlBaDetailBo bo) {
        SnhshjlBaDetail update = BeanUtil.toBean(bo, SnhshjlBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SnhshjlBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除土报告详细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
